package com.ftg.learn.xml;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.Map;

/**
 * @author 布衣
 */
public class First01 {

    public static void main(String[] args) {
        //想让这个java来完成程序 运行，解析配置文件，创建mybatis核心，然后执行sql操作数据库
        String path = "xml/configuration.xml";

        //解析图纸  Reader 读
        Reader resourceAsReader = null;
        SqlSession sqlSession = null;
        try {
            resourceAsReader = Resources.getResourceAsReader(path);

            //创建核心对象 建筑者
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

            //创建核心对象  工厂
            SqlSessionFactory sqlSessionFactory = builder.build(resourceAsReader);

            //创建核心对象  会话
            sqlSession = sqlSessionFactory.openSession();

            List<Map<String, Object>> objects = sqlSession.selectList("product.list");

            System.out.println("我们自己打印的输出 ：" + objects);


        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (resourceAsReader != null) {
                try {
                    resourceAsReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

}
